package yecq_readwrite;

import java.util.concurrent.locks.Lock;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Reader extends Thread {
    // 定义写入锁
    private Lock lock;

    // 定义构造函数
    public Reader(String name, Lock lock) {
        super(name);
        this.lock = lock;
    }

    @Override
    public void run() {
        while (true) {
            // 利用锁开关一段循环空间
            this.lock.lock();
            System.out.println(this.getName() + " 读取...");
            try {
                sleep(1000 * Util.getRandomNumber(5));
            } catch (InterruptedException ex) {
                Logger.getLogger(Reader.class.getName()).log(Level.SEVERE, null, ex);
            }
            this.lock.unlock();
        }
    }
}
